package com.srit.system.controller;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.srit.core.bean.Page;
import com.srit.forecast.constant.Constant;
import com.srit.system.bean.PageConfig;
import com.srit.system.constant.SystemConstant;
import com.srit.system.dao.CodeTypeDao;
import com.srit.system.dao.GridAttributeDao;
import com.srit.system.dao.PageConfigDao;
import com.srit.system.service.PageConfigService;

/**
 * 报文设置
 * 
 * @author guan
 * 
 */
@Controller
public class PageConfigController {
	protected static Logger log = Logger.getLogger(PageConfigController.class);
	@Autowired(required = true)
	private PageConfigService pageConfigService;

	@Autowired(required = true)
	private PageConfigDao pageConfigDao;

	@Autowired(required = true)
	private GridAttributeDao gridAttributeDao;

	@Autowired(required = true)
	private CodeTypeDao codeTypeDao;

	@RequestMapping(value = "/pageConfig-search")
	public String search(@ModelAttribute Page page,
			@ModelAttribute Object searchForm) {
		pageConfigDao.page(page);
		return "system/pageConfig";
	}

	@RequestMapping(value = "/pageConfig-form")
	public String form(@ModelAttribute PageConfig reportConfig,
			@ModelAttribute ArrayList<String> codeTypeList, ModelMap modelMap) {
		return "system/pageConfig";
	}

	@RequestMapping(value = "/pageConfig-edit")
	public String edit(@RequestParam int id,
			@ModelAttribute PageConfig pageConfig, ModelMap modelMap) {
		if (log.isDebugEnabled()) {
			log.debug("Request watherCodeId is:" + id);
		}
		BeanUtils.copyProperties(pageConfigService.getById(id), pageConfig);
		modelMap.addAttribute("gridAttributeList", gridAttributeDao.listAll());
		modelMap.addAttribute("codeTypeList", codeTypeDao.listAll());
		modelMap.addAttribute("subjectTypeArr", new String[] {
				Constant.GRID_SUBJECT_ONE, Constant.GRID_SUBJECT_MANY });
		modelMap.addAttribute("gridTypeArr", new String[] { Constant.GRID_FORM,
				Constant.GRID_LIST_H, Constant.GRID_LIST_V });
		return "system/pageConfig";
	}

	@RequestMapping(value = "/pageConfig-saveOrUpdate", method = RequestMethod.POST)
	public String saveOrUpdateReportConfig(PageConfig pageConfig) {
		if (log.isDebugEnabled()) {
			log.debug("Request ReportConfig is:" + pageConfig);
		}
		if (pageConfig.getId() != null && pageConfig.getId() != 0) {
			pageConfigDao.update(pageConfig);
		} else {
			pageConfigDao.save(pageConfig);
			pageConfig.setId(pageConfigDao.getMaxId());
		}
		// foward到查询页面
		return "forward:/pageConfig-edit?id=" + pageConfig.getId();
	}

	@RequestMapping(value = "/pageConfig-del", method = RequestMethod.POST)
	public String deleteReportConfig() {
		// 传入待删除的id
		return "forward:/pageConfig-search";
	}

	@RequestMapping(value = "/pageConfig-view")
	public String renderPage(@RequestParam int id, ModelMap modelMap) {
		PageConfig pageConfig = pageConfigService.getById(id);
		// reportConfig.(2);
		// GridAttribute attr = new GridAttribute();
		// attr.setShowType(Constant.GRID_ATTR_SELECT);
		// attr.setCodeType("WIND");
		// List<GridAttrRel> relList = new ArrayList<GridAttrRel>();
		// for (int i = 0; i < 2; i++) {
		// GridAttrRel rel = new GridAttrRel();
		// rel.setHtmlId("th_time");
		// rel.setHtmlName("th_time");
		// rel.setShowName("测试");
		// rel.setWidth("20%");
		// rel.setTitle("测试啊测试啊");
		// relList.add(rel);
		// }
		//
		// List<Grid> gridList = new ArrayList<Grid>();
		// Grid grid = new Grid();
		// grid.setEffectTime(2);
		// grid.setRelList(relList);
		// gridList.add(grid);
		//
		// pageConfig.setGridList(gridList);
		//
		// // reportConfig.setRelList(relList);
		modelMap.addAttribute(SystemConstant.PAGECONFIG, pageConfig);
		return pageConfig.getPageValue();
	}

}
